package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dto.EmpListDto;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.page.PageBean;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import com.itheima.vo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;
    @Autowired
    EmpExprMapper empExprMapper;
    @Override
    public PageBean list(EmpListDto empListDto) {

        //开启分页
        PageHelper.startPage(empListDto.getPage(),empListDto.getPageSize());
        //执行查询
        List<Emp> list = empMapper.list(empListDto);
        Page<Emp> p = (Page<Emp>) list;
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //插入员工信息
        empMapper.insert(emp);
        //获取刚刚插入员工信息的Id
        Integer empId = emp.getId();
        //获取员工经历
        List<EmpExpr> exprList = emp.getExprList();
        //对员工经历进行判空
        if(!CollectionUtils.isEmpty(exprList)){
            //如果不是空的话 给每一条经历先插入id
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            //批量插入
            empExprMapper.insertExpr(exprList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> ids) {
        //按照员工的id删除员工
        empMapper.deleteByIds(ids);
        //删除员工的同时还要删除对应的员工经历
        empExprMapper.deleteByIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        Emp emp = empMapper.getInfo(id);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //先更新修改时间
        emp.setUpdateTime(LocalDateTime.now());
        //首先 更新员工的信息
        empMapper.update(emp);
        //其次 删除这个员工之前的经历信息
        empExprMapper.deleteByIds(Arrays.asList(emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        //最后 添加新的经历信息
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setId(emp.getId()));
            empExprMapper.insertExpr(exprList);
        }

    }

    @Override
    public LoginInfo login(Emp emp) {

        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        //查询到的结果不为空说明输出账号密码正确
        if(empLogin != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
}
